//
//  RecordRnView.m
//  xmgjAppFront
//
//  Created by ZekiMini on 2020/9/16.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "RecordRnView.h"
#import <React/RCTUIManager.h>
#import <React/RCTDefines.h>
#import "GJTakeVideoView.h"
@interface RecordRnView ()
@property (nonatomic, strong) GJTakeVideoView *videoView;
@end

@implementation RecordRnView
//标记宏
RCT_EXPORT_MODULE()


#pragma mark -- method
//打开相机摄像头
RCT_EXPORT_METHOD(startCamera:(nonnull NSNumber *)reactTag){
  [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, GJTakeVideoView *> *viewRegistry) {
    GJTakeVideoView *view = viewRegistry[reactTag];
    if (![view isKindOfClass:[GJTakeVideoView class]]) {
      //      RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
    } else {
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.videoView startCamera];
      });
    }
  }];
}
//关闭相机摄像头
RCT_EXPORT_METHOD(closeCamera:(nonnull NSNumber *)reactTag){
  [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, GJTakeVideoView *> *viewRegistry) {
    GJTakeVideoView *view = viewRegistry[reactTag];
    if (![view isKindOfClass:[GJTakeVideoView class]]) {
      //      RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
    } else {
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.videoView closeCamera];
      });
    }
  }];
}

//开始摄像
RCT_EXPORT_METHOD(takeVideo:(nonnull NSNumber *)reactTag){
  [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, GJTakeVideoView *> *viewRegistry) {
    GJTakeVideoView *view = viewRegistry[reactTag];
    if (![view isKindOfClass:[GJTakeVideoView class]]) {
      //      RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
    } else {
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.videoView takeVideo];
      });
    }
  }];
}
//停止摄像
RCT_EXPORT_METHOD(stopTakeVideo:(nonnull NSNumber *)reactTag){
  [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, GJTakeVideoView *> *viewRegistry) {
    GJTakeVideoView *view = viewRegistry[reactTag];
    if (![view isKindOfClass:[GJTakeVideoView class]]) {
      //      RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
    } else {
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.videoView stopTakeVideo];
      });
    }
  }];
}

#pragma mark -- getter
- (GJTakeVideoView *)videoView{
  if (!_videoView) {
    _videoView = [GJTakeVideoView new];
  }
  return _videoView;
}
//将videoView作为管理器的view
- (UIView *)view{
  return self.videoView;
}
@end
